package com.zpy.SmartChargeStation.mqtt.client.factory;

import com.zpy.SmartChargeStation.mqtt.client.conf.MqttProps;
import com.zpy.SmartChargeStation.mqtt.client.model.MqttConstants;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.messaging.MessageChannel;

/**
 * description: 发送消息
 */

@Configuration
@EnableIntegration
@Slf4j
public class OutBoundSend {

    @Resource
    private MqttProps props;

    @Resource
    private MqttPahoClientFactory factory;

    /**
     * description: 发送通道
     */
    @Bean(name = MqttConstants.OUT_CHANNEL)
    public MessageChannel outChannel() {

        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = MqttConstants.OUT_CHANNEL)
    public MqttPahoMessageHandler outHandler() {

        MqttPahoMessageHandler messageHandler =
                new MqttPahoMessageHandler(
                        //注意这里和视频有所改动
                        props.getClientId(),
                        factory);

        log.info(">>>>>工厂模式发送消息处理器生成状态 "+messageHandler.toString());

        return messageHandler;
    }


}
